//
//  RWBChouseAgeView.m
//  Renwubang
//
//  Created by 张露杰 on 17/12/5.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "RWBChouseView.h"
#import "RWBChousePikerView.h"
@interface RWBChouseView()
@property (nonatomic, strong) RWBChousePikerView *piker;
@end

@implementation RWBChouseView
{
    //    选项数组
    NSArray *_titles;
}
- (instancetype)initWithTitles:(NSArray *)titles
{
    self = [super init];
    if (self) {
        _titles = titles;
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    self.frame = CGRectMake(0, 0, SCREENW, SCREENH - 64);
    
    /**
     底部遮罩
     */
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    [self addSubview:backGroundView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tap];
    [self addSubview:self.piker];
}

-(RWBChousePikerView *)piker
{
    if (!_piker) {
        _piker = [[RWBChousePikerView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 250*screenW_6) titles:_titles];
        
        __weak typeof(self) weakSelf = self;
        _piker.chouseTitleblock = ^(NSString *title)
        {
            if (weakSelf.chouseTitleblock) {
                weakSelf.chouseTitleblock(title);
            }
            [weakSelf dismiss];
        };
    }
    return _piker;
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0.0;
                         _piker.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 250*screenW_6);
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                     }];
}

-(void)showOnView:(UIView *)view
{
    if (!view) {
        return;
    }
    [view addSubview:self];
    [[TomamaUI getTopVC].view endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        _piker.frame = CGRectMake(0, self.frame.size.height - 250*screenW_6, self.frame.size.width, 250*screenW_6);
    } completion:nil];
}

@end
